//
//  UserInfoViewController.m
//  LoveHealth
//
//  Created by 陈永坤 on 14-9-18.
//  Copyright (c) 2014年 lanou. All rights reserved.
//

#import "UserInfoViewController.h"
#import "UserSettingViewController.h"
#import "CustomCell.h"
#import "UserCollectionViewController.h"
#import "HealthNewsDetailViewController.h"

#import "SQLHelper.h"

@interface UserInfoViewController ()

- (void)setupNavigationBar;
@end

@implementation UserInfoViewController
- (void)dealloc
{
    self.tableView = nil;
    [super dealloc];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
   
    
//    self.view.backgroundColor = [UIColor purpleColor];
    [self setupNavigationBar];
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, self.view.frame.size.width,self.view.frame.size.height - 64 - 49) style:UITableViewStyleGrouped];
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor whiteColor];
//    _tableView.alpha = 0.8;
    [self.view addSubview:_tableView];
    
    // Do any additional setup after loading the view.
    
}
#pragma mark--------------------------导航栏设置----------------------------
- (void)setupNavigationBar
{
    self.navigationItem.title = @"我的信息";
    UIBarButtonItem * rightButton = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"settingImg"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(didClickRightBarButtonItemAction:)];
    self.navigationItem.rightBarButtonItem = rightButton;
    [rightButton release];
}

- (void)didClickRightBarButtonItemAction:(UIBarButtonItem *)barButtonItem
{
    UserSettingViewController * settingVC = [[UserSettingViewController alloc] init];
    settingVC.userInfoVC = self;
    [self.navigationController pushViewController:settingVC animated:YES];
    [settingVC release];
}

#pragma mark----------------------tableView代理方法-------------------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 4;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.01;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0.01;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 0) {
        return 210;
    }
    return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.automaticallyAdjustsScrollViewInsets = NO;
    if (indexPath.row == 0) {
        static NSString * identifier = @"customCell";
        CustomCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (nil == cell) {
            cell = [[[CustomCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:identifier] autorelease];
        }
        //设置cell选中不变色
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.modalLoginBlock = ^(LoginType  type){
            
            UserLoginModalViewController * userLoginVC  = [[UserLoginModalViewController alloc]init];
            userLoginVC.modalTransitionStyle = UIModalTransitionStyleFlipHorizontal;
            userLoginVC.loginType = type;
            userLoginVC.userInfoVC = self;
            UINavigationController * userLoginNC = [[UINavigationController alloc]initWithRootViewController:userLoginVC];
            userLoginNC.navigationBar.barTintColor = UIColorCommon;
            [self presentViewController:userLoginNC animated:YES completion:nil];
            [userLoginVC release];
            [userLoginNC release];
        };

        NSUserDefaults * userDefaults = [NSUserDefaults standardUserDefaults];
        NSDictionary * dic = [userDefaults objectForKey:KLoginUserInfo];
        NSString * str = dic[kLoginAccount];
        if (dic != nil) {
            cell.headImageView.image = [UIImage imageWithData:dic[kloginImage]];
            //隐藏imageView  显示label
            cell.qqLoginImageView.hidden = YES;
            cell.sinaLoginImageView.hidden = YES;
            cell.nameLabel.text = str;
            cell.nameLabel.hidden = NO;
        }else {
            cell.qqLoginImageView.hidden =NO;
            cell.sinaLoginImageView.hidden = NO;
            cell.nameLabel.hidden = YES;
            cell.headImageView.image = [UIImage imageNamed:@"headDefault"];
        }
        return cell;
    }
    
    static NSString * identifier = @"cell";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier] autorelease];
    }
    cell.imageView.image = [UIImage imageNamed:@""];
    if (indexPath.row == 1) {
        cell.textLabel.text = @"我的资讯";
        cell.imageView.image = [UIImage imageNamed:@"tab_news"];
    }else if (indexPath.row == 2){
        cell.textLabel.text = @"我的常识";
        cell.imageView.image = [UIImage imageNamed:@"tab_knowledge"];
    }else if (indexPath.row == 3){
        cell.textLabel.text = @"我的食谱";
        cell.imageView.image = [UIImage imageNamed:@"tab_food"];
    }
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}


- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row != 0) {
        UserCollectionViewController * collectionVC = [[UserCollectionViewController alloc] init];
        
        //当点击第二个cell的时候
        if (indexPath.row == 1) {
            collectionVC.favoiteType = newsType;
            collectionVC.favoriteArray =[NSMutableArray arrayWithArray:[[SQLHelper shareInstance] selectAllWithTableName:KnewsTableName]];
        }
        if (indexPath.row == 2) {
            collectionVC.favoiteType = knowledgeType;
            collectionVC.favoriteArray =[NSMutableArray arrayWithArray:[[SQLHelper shareInstance] selectAllWithTableName:KknowledgeTableName]];
        }
        if (indexPath.row == 3) {
            collectionVC.favoiteType = foodType;
            collectionVC.favoriteArray =[NSMutableArray arrayWithArray:[[SQLHelper shareInstance] selectAllWithTableName:KfoodTableName]];
        }
        
        [self.navigationController pushViewController:collectionVC animated:YES];
        [collectionVC release];
    }
}


#pragma mark ---------------------登陆点击事件-------------------------

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
